/**
 * <pre>
 * Copyright:		Copyright(C) 2011-2012, ketayao.com
 * Filename:		com.ketayao.ketacustom.util.dwz.SpringDataJpaPageConvert.java
 * Class:			SpringDataJpaPageConvert
 * Date:			2018-09-11
 * Author:			<a href="mailto:ketayao@gmail.com">ketayao</a>
 * Version          1.1.0
 * Description:		
 *
 * </pre>
 **/
package com.fly.core.entity;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

/**
 * 解决dwz page 的遗留问题，使程序更易移植和替换
 * 
 * @author <a href="mailto:ketayao@gmail.com">ketayao</a> Version 1.1.0
 * @since 2018-09-11 下午10:03:18
 */
public class PageUtils
{
    /**
     * 生成spring data JPA 对象 描述
     * 
     * @param page
     * @return
     */
    public static Pageable createPageable(PageEntity page)
    {
        if (StringUtils.isNotBlank(page.getOrderField()))
        {
            // 忽略大小写
            if (page.getOrderDirection().equalsIgnoreCase(PageEntity.ORDER_DIRECTION_ASC))
            {
                return PageRequest.of(page.getPlainPageNum() - 1, page.getNumPerPage(), Sort.Direction.ASC, page.getOrderField());
            }
            else
            {
                return PageRequest.of(page.getPlainPageNum() - 1, page.getNumPerPage(), Sort.Direction.DESC, page.getOrderField());
            }
        }
        return PageRequest.of(page.getPlainPageNum() - 1, page.getNumPerPage());
    }
}
